package org.lmw.wechat.ui.frag;

import org.lmw.wechat.BaseFragment;
import org.lmw.wechat.R;
import org.lmw.wechat.ui.ChatActivity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;

public class MessageFragment extends BaseFragment {
	View v;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		v = inflater.inflate(R.layout.fragment_message, null);
		return v;
	}

	SwipeRefreshLayout refreshView;
	ListView listView;
	MsgAdapter adapter;

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		init();
	}

	private void init() {
		refreshView = (SwipeRefreshLayout) v.findViewById(R.id.refreshView);
		refreshView.setColorScheme(R.color.holo_blue_light, R.color.transparent, R.color.holo_blue_light, R.color.transparent);
		refreshView.setOnRefreshListener(new refreshListener());

		listView = (ListView) v.findViewById(R.id.listView);
		adapter = new MsgAdapter(getActivity());
		listView.setAdapter(adapter);
		listView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				startActivity(new Intent(getActivity(), ChatActivity.class));
			}
		});
	}

	class refreshListener implements OnRefreshListener {
		@Override
		public void onRefresh() {
			refreshView.setRefreshing(false);
		}
	}

	class MsgAdapter extends BaseAdapter {
		LayoutInflater inflater;

		public MsgAdapter(Context c) {
			inflater = LayoutInflater.from(c);
		}

		@Override
		public int getCount() {
			return 10;
		}

		@Override
		public Object getItem(int arg0) {
			return null;
		}

		@Override
		public long getItemId(int arg0) {
			return 0;
		}

		@Override
		public View getView(int arg0, View contentView, ViewGroup arg2) {
			if (contentView == null) {
				contentView = inflater.inflate(R.layout.view_item_message, null);
			}
			return contentView;
		}

	}
}
